package com.yourenbang.lingyun.transaction.web.controller.manage;

import com.yourenbang.lingyun.constants.BizCodeMessages;
import com.yourenbang.lingyun.constants.Constant;
import com.yourenbang.lingyun.core.base.controller.BaseController;
import com.yourenbang.lingyun.core.base.interceptor.ExcludeInterceptor;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.StringUtil;
import com.yourenbang.lingyun.core.util.date.LocalDateUtils;
import com.yourenbang.lingyun.transaction.web.domain.dto.manage.*;
import com.yourenbang.lingyun.transaction.web.service.manage.ManageTradeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <h3>service-transaction</h3>
 * <p>管理后台交易管理</p>
 *
 * @author : jason
 * @date : 2020-08-28 17:54
 **/

@RestController
@RequestMapping("/transaction/manage")
@Slf4j
public class ManageTradeController extends BaseController {
    @Autowired
    private ManageTradeService manageTradeService;

    @ExcludeInterceptor
    @PostMapping(value = "/trade/list")
    public Result tradeList(@Validated @RequestBody TradeManageDTO dto, BindingResult br) {

        KLog.info("[后台交易列表]入参:{}", dto.toString());
        Result verifyParam = verifyParam(br);
        if (!verifyParam.isSuccess()) {
            return verifyParam;
        }
        if(StringUtils.isNotBlank(dto.getTradeEndTime())) {
            dto.setTradeEndTime(LocalDateUtils.formatByDefaultFormat(LocalDateUtils.parseLocalDateByDefaultFormat(dto.getTradeEndTime()).plusDays(1)));
        }
        String a9UserBaseId = getHttpServletRequest().getHeader(Constant.MANAGE_A9_UID_HEADER_NAME);
        if (StringUtils.isNotBlank(a9UserBaseId)) {
            dto.setA9UserBaseId(a9UserBaseId);
        }

        Result result = manageTradeService.tradeList(dto);

        return result;
    }

    @ExcludeInterceptor
    @PostMapping(value = "/trade/detail")
    public Result tradeDetail(@Validated @RequestBody TradeManageDetailDTO dtoRequest,
                              BindingResult br) {

        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        String a9UserBaseId = getHttpServletRequest().getHeader(Constant.MANAGE_A9_UID_HEADER_NAME);
        if (StringUtils.isNotBlank(a9UserBaseId)) {
            dtoRequest.setA9UserBaseId(a9UserBaseId);
        }

        Result result1 = manageTradeService.tradeDetail(dtoRequest);

        return result1;
    }

   /**
    * 补录交易
    * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13239">火眼接口连接</a>
    */
    @ExcludeInterceptor
    @PostMapping(value = "/trade/supplement")
    public Result tradeSupplement(@Validated @RequestBody TradeSupplementDTO tradeSupplementDTO,
                                  BindingResult br) {

        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }

        return manageTradeService.tradeSupplement(tradeSupplementDTO);

    }

    /**
     * 批量补录交易模板
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13245">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @GetMapping(value = "/batch/trade/supplement/template")
    public void batchTradeSupplementTemplate() {

        manageTradeService.batchTradeSupplementTemplate();

    }

    /**
     * 批量补录交易
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13242">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/batch/trade/supplement")
    public Result batchTradeSupplement(@RequestParam("files")MultipartFile file,
                                       @RequestParam("operator") String operator) {

        return manageTradeService.batchTradeSupplement(file,operator);

    }

    /**
     * 批量补录交易 M40
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13242">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/batch/trade/supplement/m40")
    public Result batchTradeSupplementM40(@RequestParam("files")MultipartFile file,
                                          @RequestParam("operator") String operator,
                                          @RequestParam(value = "address", required = false) String mail) {
        if(StringUtil.isNotEmpty(mail) && !mail.endsWith("@kaolakuaishou.com")){
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "请使用公司内部邮箱");
        }
        return manageTradeService.batchTradeSupplementM40(file,operator, mail);

    }
    /**
     * 补录结果列表
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13248">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/trade/supplement/result/list")
    public Result tradeSupplementResultList(@RequestBody TradeSupplementResultListDTO tradeSupplementResultListDTO) {

        return manageTradeService.tradeSupplementResultList(tradeSupplementResultListDTO);

    }

    /**
     * 获取所有卡类型
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13368">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @GetMapping(value = "/get/all/card/flag")
    public Result getAllCardFlag() {

        return manageTradeService.getAllCardFlag();

    }

    /**
     * 修复卡类型
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13617">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/correct/card/flag")
    public Result correctCardFlag(@Validated @RequestBody CorrectCardFlagDTO correctCardFlagDTO, BindingResult br) {

        KLog.info("[修复卡类型]入参:{}", correctCardFlagDTO.toJSON());
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }

        return manageTradeService.correctCardFlag(correctCardFlagDTO);

    }

    /**
     * 获取所有费率类型
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13677">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @GetMapping(value = "/get/all/rate")
    public Result getAllRate() {

        return manageTradeService.getAllRate();

    }

    /**
     * 获取所有交易状态
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/13710">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @GetMapping(value = "/get/all/transaction/status")
    public Result getAllTransactionStatus() {

        return manageTradeService.getAllTransactionStatus();

    }

}
